package com.aaa.sbms.service;

import com.aaa.sbms.dao.RecruitDao;
import com.aaa.sbms.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RecruitServiceImpl implements RecruitService{

    @Autowired(required = false)
    private RecruitDao recruitDao;


    @Override
    public Map depts() {
        Map resultMap = new HashMap();
        List<Map> depts = recruitDao.depts();
        resultMap.put("list",depts);
        return resultMap;
    }

    @Override
    public Map positions() {
        Map resultMap = new HashMap();
        List<Map> positions = recruitDao.positions();
        resultMap.put("list",positions);
        return resultMap;
    }

    @Override
    public Map getList(Map map) {
        int pageNo = Integer.valueOf(map.get("pageNo").toString());
        int pageSize = Integer.valueOf(map.get("pageSize").toString());
        int begin = (pageNo-1)*pageSize;
        map.put("begin",begin);
        map.put("pageSize",pageSize);
        List<Map> page = recruitDao.getPage(map);
        int total = recruitDao.getPageCount(map);
        HashMap<Object,Object> resultMap= new HashMap<>();
        resultMap.put("list",page);
        resultMap.put("total",total);
        return resultMap;
    }

    @Override
    public Map recruitPage(Map map) {
        int pageNo = Integer.valueOf(map.get("pageNo").toString());
        int pageSize = Integer.valueOf(map.get("pageSize").toString());
        int begin = (pageNo-1)*pageSize;
        map.put("begin",begin);
        map.put("pageSize",pageSize);
        List<Map> page = recruitDao.recruitPage(map);
        int total = recruitDao.recruitPageCount(map);
        HashMap<Object,Object> resultMap= new HashMap<>();
        resultMap.put("list",page);
        resultMap.put("total",total);
        return resultMap;
    }

    @Override
    public Map recruitRecordPage(Map map) {
        int pageNo = Integer.valueOf(map.get("pageNo").toString());
        int pageSize = Integer.valueOf(map.get("pageSize").toString());
        int begin = (pageNo-1)*pageSize;
        map.put("begin",begin);
        map.put("pageSize",pageSize);
        List<Map> page = recruitDao.recruitRecordPage(map);
        int total = recruitDao.recruitRecordPageCount(map);
        HashMap<Object,Object> resultMap= new HashMap<>();
        resultMap.put("list",page);
        resultMap.put("total",total);
        return resultMap;
    }

    @Override
    public Map empPage(Map map) {
        int pageNo = Integer.valueOf(map.get("pageNo").toString());
        int pageSize = Integer.valueOf(map.get("pageSize").toString());
        int begin = (pageNo-1)*pageSize;
        map.put("begin",begin);
        map.put("pageSize",pageSize);
        List<Map> page = recruitDao.empPage(map);
        int total = recruitDao.empPageCount(map);
        HashMap<Object,Object> resultMap= new HashMap<>();
        resultMap.put("list",page);
        resultMap.put("total",total);
        return resultMap;
    }

    @Override
    public Map recruitInfoOne(int id) {
        Map map = recruitDao.recruitInfoOne(id);
        Map resultMap = new HashMap();
        resultMap.put("list",map);
        return resultMap;
    }

    @Override
    public Map recruitOne(int id) {
        Map resultMap = new HashMap();
        Map map = recruitDao.recruitOne(id);
        if (map.get("filepath")==null){
            resultMap.put("filepath",0);
        }else {
            String filepath = map.get("filepath").toString();
            resultMap.put("filepath",filepath);
        }
        resultMap.put("list",map);
        return resultMap;
    }

    @Override
    public int addRecruitInfo(Map map) {
        return recruitDao.addRecruitInfo(map);
    }

    @Override
    public int addRecruit(Map map) {
        String pname = map.get("pname").toString();
        String dname = map.get("dname").toString();
        int deptid = recruitDao.deptidByDname(dname);
        int pid = recruitDao.pidByPname(pname);
        map.put("positionid",pid);
        map.put("deptid",deptid);
        return recruitDao.addRecruit(map);
    }

    @Override
    public int updateRecruitInfo(Map map) {
        return recruitDao.updateRecruitInfo(map);
    }

    @Override
    public int deleteRecruitInfo(int id) {
        return recruitDao.deleteRecruitInfo(id);
    }

    @Override
    @Transactional
    public ResultUtil accept(Map map) {
        try {
            int idre = Integer.valueOf(map.get("id").toString());
            String dname = map.get("dname").toString();
            String pname = map.get("pname").toString();
            int deptid = recruitDao.deptidByDname(dname);
            int positionid = recruitDao.pidByPname(pname);
            //判断招聘信息表所需人数是否已满
            Map map1 = recruitDao.findRecruitInfoOfNumberByDeptidAndPositionid(deptid, positionid);
            if (Integer.valueOf(map1.get("number").toString())==0){
                return new ResultUtil(2002,"人数已满",null);
            }
            //如果未满
            int id = Integer.valueOf(map1.get("id").toString());
            map.put("deptid",deptid);
            map.put("positionid",positionid);
            map.put("result",1);
            //添加信息到users表
            recruitDao.addUser(map);

            //添加信息到checkins宿舍表


            //添加入职审核记录
            recruitDao.addRecord(map);
            //修改招聘申请表状态为入职成功3
            recruitDao.updateRecruitOfStatus3(idre);
            //修改招聘信息表的所需人数-1
            recruitDao.updateRecruitInfoOfNumber(id);
            return new ResultUtil(2001,"业务成功",null);
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚
            return new ResultUtil(2002,"抓到异常",null);
        }
    }

    @Override
    @Transactional
    public ResultUtil reject(Map map) {
        try {
            String dname = map.get("dname").toString();
            String pname = map.get("pname").toString();
            int deptid = recruitDao.deptidByDname(dname);
            int positionid = recruitDao.pidByPname(pname);
            //判断人数是否已满
            Map map1 = recruitDao.findRecruitInfoOfNumberByDeptidAndPositionid(deptid, positionid);
            if (Integer.valueOf(map1.get("number").toString())==0){
                return new ResultUtil(2002,"人数已满",null);
            }
            //如果没满
            map.put("deptid",deptid);
            map.put("positionid",positionid);
            map.put("result",0);
            //添加入职审核记录
            recruitDao.addRecord(map);
            int id = Integer.valueOf(map.get("id").toString());
            //更新入职申请表的状态为驳回2
            recruitDao.updateRecruitOfStatus2(id);
            return new ResultUtil(2001,"已驳回",null);
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚
            return new ResultUtil(2002,"抓到异常",null);
        }
    }
}
